package com.cskaoyan.service;

import com.cskaoyan.bean.data.*;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.wxbo.CollectDataList;
import com.cskaoyan.bean.wxbo.WxCollectBO;
import com.cskaoyan.bean.wxvo.CollectList;
import com.cskaoyan.bean.wxvo.WxCollectVO;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceIpImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Override
    public UserData selectUserData(Integer page, Integer limit, String sort, String order, String username, String mobile) {
        //查询之前，使用分页插件
        PageHelper.startPage(page, limit);
        UserExample userExample = new UserExample();
        //排序信息
        userExample.setOrderByClause(sort + " " + order);
        UserExample.Criteria criteria = userExample.createCriteria();
        //查询所有的用户 模糊查询
        if (username != null && !username.trim().equals("")) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !mobile.trim().equals("")) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<User> users = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        long total = pageInfo.getTotal();
        UserData userData = new UserData();
        userData.setTotal((int) total);
        userData.setItems(users);
        return userData;
    }

    @Override
    public AddressData selectAddressData(Integer page, Integer limit, String sort, String order, Integer userId, String name) {
        PageHelper.startPage(page, limit);
        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause(sort + " " + order);
        AddressExample.Criteria criteria = addressExample.createCriteria();
        //根据用户id和收货人姓名查询
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (name != null && !name.trim().equals("")) {
            criteria.andNameLike("%" + name + "%");
        }
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        PageInfo<Address> pageInfo = new PageInfo<>(addresses);
        long total = pageInfo.getTotal();
        AddressData addressData = new AddressData();
        addressData.setItems(addresses);
        addressData.setTotal((int) total);
        return addressData;
    }

    @Override
    public CollectData selectCollectData(Integer page, Integer limit, String sort, String order, Integer id, Integer userId) {
        PageHelper.startPage(page, limit);
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);
        CollectExample.Criteria criteria = collectExample.createCriteria();
        //根据用户id和商品id查询
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<Collect> pageInfo = new PageInfo<>(collects);
        long total = pageInfo.getTotal();
        CollectData collectData = new CollectData();
        collectData.setItems(collects);
        int total1 = (int) total;
        collectData.setTotal(total1);
        return collectData;
    }

    @Override
    public FootprintData selectFootprintData(Integer page, Integer limit, String sort, String order, Integer goodsId, Integer userId) {
        PageHelper.startPage(page, limit);
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> pageInfo = new PageInfo<>(footprints);
        long total = pageInfo.getTotal();
        FootprintData footprintData = new FootprintData();
        footprintData.setTotal((int) total);
        footprintData.setItems(footprints);
        return footprintData;
    }

    @Override
    public SearchHistoryData selectSearchHistoryData(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        PageHelper.startPage(page, limit);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(sort + " " + order);
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !keyword.trim().equals("")) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> pageInfo = new PageInfo<>(searchHistories);
        long total = pageInfo.getTotal();
        SearchHistoryData searchHistoryData = new SearchHistoryData();
        searchHistoryData.setTotal((int) total);
        searchHistoryData.setItems(searchHistories);
        return searchHistoryData;
    }

    @Override
    public FeedBackData selectFeedBackData(Integer page, Integer limit, String sort, String order, Integer id, String username) {
        PageHelper.startPage(page, limit);
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        feedbackExample.setOrderByClause(sort + " " + order);
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null && username.trim().equals("")) {
            criteria.andUsernameLike("%" + username + "%");
        }
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> pageInfo = new PageInfo<>(feedbacks);
        long total = pageInfo.getTotal();
        FeedBackData feedBackData = new FeedBackData();
        feedBackData.setTotal((int) total);
        feedBackData.setItems(feedbacks);
        return feedBackData;
    }

    @Override
    public DashboardData selectDashboardData(DashboardData dashboardData) {
        //订单数量
        int orderTotal = dashboardData.getOrderTotal();
        OrderExample orderExample = new OrderExample();
        long l = orderMapper.countByExample(orderExample);
        orderTotal = (int) l;
        //用户数量
        int userTotal = dashboardData.getUserTotal();
        UserExample userExample = new UserExample();
        long l1 = userMapper.countByExample(userExample);
        userTotal = (int) l1;
        //商品数量
        int goodsTotal = dashboardData.getGoodsTotal();
        GoodsExample goodsExample = new GoodsExample();
        long l2 = goodsMapper.countByExample(goodsExample);
        goodsTotal = (int) l2;
        //货品数量
        int productTotal = dashboardData.getProductTotal();
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        long l3 = goodsProductMapper.countByExample(goodsProductExample);
        productTotal = (int) l3;
        dashboardData.setGoodsTotal(goodsTotal);
        dashboardData.setOrderTotal(orderTotal);
        dashboardData.setUserTotal(userTotal);
        dashboardData.setProductTotal(productTotal);
        return dashboardData;
    }

    @Override
    public int selectValueIdByUserId(Integer id) {

        return userMapper.queryValueIdByUserId(id);
    }

    @Override
    public int selectTypeByUserId(Integer id) {
        return userMapper.queryTypeByUserId(id);
    }

    //根据userid collect表中查到id
    @Override
    public int selectIdByUserId(Integer id) {
        return userMapper.queryIdByUserId(id);
    }

    @Override
    public String selectNameById(int id) {
        return userMapper.queryNameById(id);
    }

    @Override
    public String selectBriefById(int id) {
        return userMapper.queryBriefById(id);
    }

    @Override
    public String selectPicUrlById(int id) {
        return userMapper.queryPicUrlById(id);
    }

    @Override
    public BigDecimal selectRetailPriceById(int id) {

        //System.out.println(id);
        BigDecimal v = userMapper.queryRetailPriceById(id);

        return v;
    }

    @Override
    public User selectUserByName(String principal) {
        User user = null;
        if (!ObjectUtils.isEmpty(principal)) {
            user = userMapper.selectOneByUserName(principal);
        }
        return user;
    }

    @Override
    public int addOrDelete(Integer type, Integer valueId, Integer userId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andValueIdEqualTo(valueId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() > 0) {
            //记录已存在 删除操作
            Collect collect = new Collect();
            collect.setDeleted(true);
            //collectMapper.updateByExampleSelective(collect, collectExample);
            collectMapper.deleteByExample(collectExample);
            return 1;
        } else {
            //记录不存在，则添加进去
            Collect collect = new Collect();
            collect.setType((byte) 0);
            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setAddTime(new Date());
            collect.setUpdateTime(new Date());
            collectMapper.insertSelective(collect);
            return 0;
        }
    }

    @Override
    public WxCollectVO collectList(Integer userId, WxCollectBO wxCollectBO) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        //根据userid查询他的藏品
        collectExample.setOrderByClause("update_time" + " " + "desc");
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        ArrayList<CollectList> collectLists = new ArrayList<>();
        //Collect collect1 = new Collect();
        for (Collect collect : collects) {
            CollectList collectList = new CollectList();
            //拿到valueid再去goods表里查询
            Integer valueId = collect.getValueId();
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
            criteria1.andIdEqualTo(valueId);
            //collect表里的valueid对应goods表里的id，实际根据主键查询只能查到一个
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);
            collectList.setBrief(goods.get(0).getBrief());
            collectList.setValueId(goods.get(0).getId());
            collectList.setName(goods.get(0).getName());
            collectList.setPicUrl(goods.get(0).getPicUrl());
            collectList.setRetailPrice(goods.get(0).getRetailPrice());
            collectList.setType(wxCollectBO.getType());
            collectList.setId(null);
            collectLists.add(collectList);
        }
        WxCollectVO wxCollectVO = new WxCollectVO();
        wxCollectVO.setTotalPages(wxCollectBO.getPage());
        wxCollectVO.setCollectList(collectLists);
        return wxCollectVO;
    }


}
